package com.britoso;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import javax.swing.table.TableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SearchThread implements Runnable
{
	TableModel model;
	int row;
	static final String apiurl = "http://www.omdbapi.com/?";//"http://www.deanclatworthy.com/imdb/?";
	static final String dataType = "&r=XML";

	public SearchThread(TableModel model, int row)
	{
		this.model = model;
		this.row=row;
	}

	public void run()
	{

		String movieKeyword;
		String url = "";
		
		movieKeyword = (String) model.getValueAt(row, 1);
		
		try
		{
			String year=(String)model.getValueAt(row, 3);
			if (year!=null && year.length()==4)
			{
				url = apiurl + "t="+URLEncoder.encode(movieKeyword.trim(), "UTF-8") +"&y="+year+ dataType;
			}
			else
			{
				url = apiurl + "t="+URLEncoder.encode(movieKeyword.trim(), "UTF-8") + dataType;
			}
		}
		catch (UnsupportedEncodingException e1)
		{
			System.out.println("Error Encoding '" + movieKeyword.trim() + "'");
			e1.printStackTrace();
		}
		//System.out.println("Thread "+row + " fetching: " + url);
		//System.out.println();

		try
		{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new URL(url).openStream());
			doc.getDocumentElement().normalize();

			//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			NodeList nList = doc.getElementsByTagName("movie");
			if(nList.getLength()==0)
			{
				//System.out.println("Thread "+row + ", Not Found: "+movieKeyword);
			}
			for (int temp = 0; temp < nList.getLength(); temp++)
			{
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE)
				{
					//System.out.println("Thread "+row + ", Found.");
					Element eElement = (Element) nNode;
					NamedNodeMap attributeMap = eElement.getAttributes();
				      
					//{2"Title","Year","Rating", "Genre","Runtime","Language","Country","Link"};
					model.setValueAt(attributeMap.getNamedItem("title").getNodeValue(), row, 2);
					model.setValueAt(attributeMap.getNamedItem("year").getNodeValue(), row, 3);
					model.setValueAt(attributeMap.getNamedItem("imdbRating").getNodeValue(), row, 4);
					model.setValueAt(attributeMap.getNamedItem("genre").getNodeValue(), row, 5);
					model.setValueAt(attributeMap.getNamedItem("runtime").getNodeValue(), row, 6);
					model.setValueAt(attributeMap.getNamedItem("plot").getNodeValue(), row, 7);
					model.setValueAt(attributeMap.getNamedItem("imdbVotes").getNodeValue(), row, 8);
					model.setValueAt("http://www.imdb.com/title/" + attributeMap.getNamedItem("imdbID").getNodeValue(), row, 9);
					//available: plot,poster,rated
				}
			}
		}
		catch (org.xml.sax.SAXParseException e2)
		{
			System.err.println("API Limit exceeded? ");
			//e2.printStackTrace();
			//return;
		}
		catch (ParserConfigurationException e2)
		{
			e2.printStackTrace();
		}
		catch (MalformedURLException e1)
		{
			e1.printStackTrace();
		}
		catch (SAXException e1)
		{
			e1.printStackTrace();
		}
		catch (IOException e1)
		{
			e1.printStackTrace();
		}
		//System.out.println("Thread "+row + " Completed.");
	}
	

}